#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

sudo dpkg --add-architecture armhf
"${DIRECTORY}/pkg-install" "libudev1:armhf libgconf-2-4:armhf libgtk2.0-0:armhf libxrender1:armhf libxtst6:armhf libxi6:armhf libxss1:armhf libnss3:armhf libasound2:armhf libstdc++6:armhf libstdc++6:armhf libstdc++6:armhf" "$(dirname "$0")" || error "Failed to install dependencies!"
sudo ln -s /usr/lib/aarch64-linux-gnu/libudev.so.1 /usr/lib/aarch64-linux-gnu/libudev.so.0
wget -O $HOME/Temps.zip https://github.com/musicpro/temps/releases/download/0.7.4/Temps-linux-armv7l.zip || error 'Failed to download Temps!'
unzip $HOME/Temps.zip || error 'Failed to unzip Temps.zip!'
sudo ln -sf $HOME/Temps-linux-armv7l/Temps /usr/local/bin/temps || error 'Failed to create a symbolic link from $HOME/Temps-linux-armv7l/Temps to /bin/temps!'
echo "[Desktop Entry]
Name=Temps
Comment=Temps is a modern and minimal menubar application based on Electron with actual weather information and forecast.
GenericName=Weather App
Exec=temps
Icon=$(dirname $0)/icon-64.png
StartupNotify=true
Terminal=false
Type=Application
Categories=Utility;" > $HOME/.local/share/applications/Temps.desktop || error 'Failed to create menu shortcut!'
rm -f $HOME/Temps.zip || sudo rm -f $HOME/Temps.zip || error 'Failed to remove Temps.zip!'
